<template>
  <div>
    <el-dialog
      title="添加判断题"
      :visible.sync="addVisible"
      width="60%"
      :show-close="false"
      :close-on-click-modal="false">

        <el-form :model="DataForm" status-icon :rules="DataForm_rules" ref="DataForm" label-width="60px" class="demo-ruleForm">
          <el-form-item label="科目：" prop="f_sb_id">
            <div class="block">
              <el-cascader
                placeholder="请选择"
                :options="options"
                filterable
                v-model="DataForm.f_sb_id"
              ></el-cascader>
            </div>
          </el-form-item>
          <div v-for="(obj,index) in DataForm.formDataList" :key="index">
           <el-row>
             <el-col :span="15">
               <el-form-item label="题目:" prop="j_title">
                 <el-input type="textarea" v-model="DataForm.formDataList[index].j_title" auto-complete="off"></el-input>
               </el-form-item>
             </el-col>
             <el-col :span="5">
               <el-form-item label="答案:" prop="ja_answer">
                 <el-radio v-model="DataForm.formDataList[index].judgmentAnswer.ja_answer" label="1">√</el-radio>
                 <el-radio v-model="DataForm.formDataList[index].judgmentAnswer.ja_answer" label="2">×</el-radio>
               </el-form-item>
             </el-col>
             <el-col :span="3">
               <el-button style="float: right; padding: 3px 0;margin-left: 15px;" type="danger" size="mini" @click="deleteDataForm(index)">删除</el-button>
               <el-button style="float: right; padding: 3px 0" type="primary" size="mini" @click="addDataForm(index)">增加</el-button>
             </el-col>
           </el-row>
          </div>
        </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog" size="mini">取 消</el-button>
        <el-button type="primary" @click="submitData" size="mini">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
  import {getAllSubject,addJudgment} from '@/api/questions'
  export default{
    props:["showAddVisible"],
    data(){
      return {
        addVisible:this.showAddVisible,
        DataForm:{
          formDataList:[
            {j_title:"",sb_id:0,judgmentAnswer:{ja_answer:""}}
          ],
          f_sb_id:0
        },
        options:[], //选择所属科目
        DataForm_rules:{
//          j_title:[
//            {type:"string",required: true, message: '不能为空', trigger: 'change' }
//          ],
//          ja_answer:[
//            {type:"string",required: true, message: '不能为空', trigger: 'change' }
//          ]
        }

      }
    },
    mounted(){
        this.c_getAllSubject();
    },
    methods:{
      //关闭弹出框
      closeDialog(){
          this.addVisible = false;
          this.$emit("closeDialog","add");
      },

      //提交
      submitData(){
        this.$refs["DataForm"].validate((valid) =>{
           if(valid){
               for(let i = 0;i < this.DataForm.formDataList.length;i ++){
                 this.DataForm.formDataList[i].sb_id = parseInt(this.DataForm.f_sb_id[2])
               }
             addJudgment(this.DataForm.formDataList).then(res=>{
                 if(res.code == 200){
                   this.$message({
                     type: 'success',
                     message: '添加成功'
                   });
                   this.closeDialog();
                 }else{
                   this.$message({
                     type: 'error',
                     message: '添加失败'
                   });
                 }
             });
           }else{

           }
        });
      },

      addDataForm(index){
          this.DataForm.formDataList.push({j_title:"",sb_id:0,judgmentAnswer:{ja_answer:""}})
      },

      deleteDataForm(index){
          if(this.DataForm.formDataList.length > 1){
            this.DataForm.formDataList.splice(index,1)
          }
      },

      c_getAllSubject(){
        getAllSubject().then(res=>{
          if(res.code == 200){
            for(let i = 0;i < res.data.length;i ++){
              let tLevel = {value:res.data[i].l_id,label:res.data[i].l_name};
              this.options.push(tLevel);
              if(res.data[i].list != null && res.data[i].list.length > 0){
                let gList = res.data[i].list;
                let children = [];
                for(let j = 0;j < gList.length;j ++){
                  let gGroup = {value:gList[j].g_id,label:gList[j].g_name};
                  children.push(gGroup);

                  if(gList[j].list != null && gList[j].list.length > 0){
                    let sList = gList[j].list;
                    let children1 = [];

                    for(let n = 0;n < sList.length;n ++){
                      let subject = {value:sList[n].sb_id,label:sList[n].sb_name};
                      children1.push(subject);
                    }
                    gGroup.children = children1;
                  }
                }
                tLevel.children = children;
              }
            }
          }
        });
      },
    }
  }
</script>
